defmodule HandlerTest do
  use ExUnit.Case
  import Servy.Handler, only: [handle: 1]

  def remove_extra_while_spaces(string) do
    String.replace(string, ~r/\s+/, " ")
  end

  test "GET /wildthings" do
    request = """
    GET /wildthings HTTP/1.1
    Cache-Control: no-cache
    Accept: */*
    User-Agent: Elixir
    Host: baidu.com
    """

    response = handle(request)

    assert response == """
    HTTP/1.1 200 OK
    Content-Type: text/html
    Content-Length: 20

    Bears, Lions, Tigers
    """
  end

  test "GET /bears" do
    request = """
    GET /bears HTTP/1.1
    Cache-Control: no-cache
    Accept: */*
    User-Agent: Elixir
    Host: baidu.com
    """

    response = handle(request)

    assert remove_extra_while_spaces(response) == "HTTP/1.1 200 OK Content-Type: text/html Content-Length: 159 <h1> All the bears!</h1> <ul> <li>Rosie - Black</li> <li>Smokey - Black</li> <li>Snow - Polar</li> <li>Teddy - Brown</li> </ul> "
  end

  test "GET /api/bears" do
    request = """
    GET /api/bears HTTP/1.1
    Cache-Control: no-cache
    Accept: */*
    User-Agent: Elixir
    Host: baidu.com
    """

    response = handle(request)

    assert remove_extra_while_spaces(response) == "HTTP/1.1 200 OK Content-Type: application/json Content-Length: 235 [{\"type\":\"Brown\",\"name\":\"Teddy\",\"id\":1,\"hibernating\":true},{\"type\":\"Black\",\"name\":\"Smokey\",\"id\":2,\"hibernating\":false},{\"type\":\"Polar\",\"name\":\"Snow\",\"id\":3,\"hibernating\":false},{\"type\":\"Black\",\"name\":\"Rosie\",\"id\":4,\"hibernating\":true}] "
  end

  test "GET /bigfoot" do
    request = """
    GET /bigfoot HTTP/1.1
    Cache-Control: no-cache
    Accept: */*
    User-Agent: Elixir
    Host: baidu.com
    """

    response = handle(request)

    assert response == "HTTP/1.1 404 Not Found\nContent-Type: text/html\nContent-Length: 17\n\nNo /bigfoot here!\n"
  end

  test "GET /bears/1" do
    request = """
    GET /bears/1 HTTP/1.1
    Cache-Control: no-cache
    Accept: */*
    User-Agent: Elixir
    Host: baidu.com
    """

    response = handle(request)

    assert remove_extra_while_spaces(response) == "HTTP/1.1 200 OK Content-Type: text/html Content-Length: 105 <h1>Show Bear</h1> <p> Bear Brown : Teddy </p> <p> Is Teddy hibernating? <strong>true</strong> </p> "
  end

  test "GET /wildlife" do
    request = """
    GET /wildlife HTTP/1.1
    Cache-Control: no-cache
    Accept: */*
    User-Agent: Elixir
    Host: baidu.com
    """

    response = handle(request)

    assert response == """
    HTTP/1.1 200 OK
    Content-Type: text/html
    Content-Length: 20

    Bears, Lions, Tigers
    """
  end

  test "GET /about" do
    request = """
    GET /about HTTP/1.1
    Cache-Control: no-cache
    Accept: */*
    User-Agent: Elixir
    Host: baidu.com
    """

    response = handle(request)

    assert response == "HTTP/1.1 200 OK\nContent-Type: text/html\nContent-Length: 134\n\n<h1> EdmondFrank </h1>\n\n<blockquote>\n  Although the world is full of suffering, it is full also of the overcoming of it\n</blockquote>\n\n"
  end

  test "POST /bears" do
    request = """
    POST /bears HTTP/1.1
    Cache-Control: no-cache
    Accept: */*
    User-Agent: Elixir
    Host: baidu.com
    Content-Type: application/x-www-form-urlencoded
    Content-Length: 21

    name=Baloo&type=Brown
    """

    response = handle(request)
    assert response == "HTTP/1.1 201 Created\nContent-Type: text/html\nContent-Length: 32\n\nCreate a Brown bear named Baloo!\n"
  end
end
